/******************************************************************************\
 * Filename   : enc_box.c
 * Copyright  : 
 * Created    : 2018-10-31 by litao
 * Description:
 * 
\******************************************************************************/
#include "enc_vi.h"
#include "enc_vpss.h"
#include "enc_venc.h"
#include "enc_aenc.h"

#include "enc_stream.h"
#include "enc_output.h"

#include "enc_osd.h"

enc_main_data main_data;

//extern void * enc_osd_qr_show(void *p_this);

int enc_hi_sys_init(void)
{
	int i;
	int ret;
	U32 block_size;
	VB_CONF_S vb_config;

    /******************************************
     step  1: init sys variable 
    ******************************************/
    memset(&vb_config,0,sizeof(VB_CONF_S));
	
    vb_config.u32MaxPoolCnt = VB_MAX_POOLS;

	for (i=0; i<ENC_MAX_CHANNEL; i++) {
	    block_size = enc_sys_CalcPicVbBlkSize(ENC_SYS_ALIGN_WIDTH, COMPRESS_MODE_SEG);
	    vb_config.astCommPool[i].u32BlkSize = block_size;
	    vb_config.astCommPool[i].u32BlkCnt = 32;		
	}

    /******************************************
     step 2: mpp system init. 
    ******************************************/
    ret = enc_sys_init(&vb_config);
    if (HI_SUCCESS != ret)
    {
        enc_err("system init failed with %d!\n", ret);
        return -1;
    }

	return 0;
}

void enc_hi_sys_uninit(void)
{
	enc_sys_exit();
}

int enc_stop(void)
{
	enc_venc_UnBindVpss(main_data.chan_mapnum);
	enc_venc_stop(main_data.chan_mapnum);
	enc_vi_UnBindVpss(main_data.chan_mapnum);
	enc_vpss_stop(main_data.chan_mapnum);
	enc_vi_stop(main_data.chan_mapnum);

	//enc_hi_sys_uninit();
	enc_osd_all_destory();

	hienc_stop_osd();

	return 0;
}

int enc_start(stENC_DEVSET_SLAVE *p_slave)
{
	int ret;

	enc_info("src chan %d -> map chan %d\n", main_data.chan_srcnum, main_data.chan_mapnum);
	
	if (!p_slave->enc_chlshare.sys_init) {
		ret = enc_hi_sys_init();
		if (ret != HI_SUCCESS) {
			return -1;
		}
		p_slave->enc_chlshare.sys_init = 1;
	}

	enc_stop();

	ret = enc_vi_start(main_data.chan_mapnum, &main_data.hdmi_dev.vi_info);
	if (ret != 0) {
        enc_err("enc_vi_start error with %d!\n", ret);
        return -1;		
	}

	ret = enc_vpss_start(main_data.chan_mapnum, &p_slave->enc_chls[main_data.chan_srcnum].encchl_vid);
	if (ret != 0) {
        enc_err("enc_vpss_start error with %d!\n", ret);
        return -1;		
	}	
	
	ret = enc_vi_BindVpss(main_data.chan_mapnum);
	if (ret != 0) {
        enc_err("enc_vi_BindVpss error with %d!\n", ret);
        return -1;		
	}

	ret = enc_venc_start(main_data.chan_mapnum, &p_slave->enc_chls[main_data.chan_srcnum].encchl_vid);
	if (ret != 0) {
        enc_err("enc_venc_start error with %d!\n", ret);
        return -1;		
	}	

	ret = enc_venc_BindVpss(main_data.chan_mapnum);
	if (ret != 0) {
        enc_err("enc_vi_BindVpss error with %d!\n", ret);
        return -1;		
	}

	main_data.enc_status = 1;

	return 0;
}

int enc_init(int chan_num)
{
	memset(&main_data, 0, sizeof(main_data));

	main_data.chan_srcnum = chan_num;
	main_data.chan_mapnum = enc_sys_ChanMap(chan_num);

	initial_qlist_body(&main_data.avout_queue);
	
	enc_hdmi_init();

	return 0;
}

int enc_start_thread(void)
{
   /* if(pthread_create(&main_data.ain_thread_id, NULL, enc_audio_input, NULL) < 0)
    {
        enc_err("failed creating audio input thread\n");
		return -1;
    }*/	

    if(pthread_create(&main_data.stm_thread_id, NULL, enc_stream_handle, NULL) < 0)
    {
        enc_err("failed creating get stream thread\n");
		return -1;
    }	

    if(pthread_create(&main_data.out_thread_id, NULL, enc_output_thread, NULL) < 0)
    {
        enc_err("failed creating output ts thread\n");
		return -1;
    }

    hienc_start_osd();

	return 0;
}
